package com.xqiapp.webs.controller;

import com.xqiapp.bean.Resultx;
import com.xqiapp.entity.User;
import com.xqiapp.utils.HttpUtil;
import com.xqiapp.utils.JsonUtil;
import com.xqiapp.webs.bean.Gender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by rwrwd7 on 2017/9/8.
 */

@Controller
public class HelloController {

    @RequestMapping("sayHello")
    public String sayHello(){
        return "Hello, spring boot";
    }

    @RequestMapping("thymeleafHello")
    public String thymeleafHello(ModelMap map){
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://xqiapp.com");
        User user = new User(UUID.randomUUID().toString(),"show","show");
        user.setGender(Gender.female.toString());
        map.addAttribute("user",user);

        map.addAttribute("time",new Date());

        List<User> list = new ArrayList<>();
        User u = null;
        for(int i = 0;i < 10;i++){
            u = new User(UUID.randomUUID().toString(),"user"+i,"pass"+i);
            list.add(u);
        }

        map.addAttribute("userList",list);
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";
    }

    @RequestMapping("submit")
    @ResponseBody
    public String submit(User user) {
        String info = "[ username:"+user.getUsername()+", password:"+user.getPassword()+"]";
        return info;
    }

    @RequestMapping("get")
    @ResponseBody
    public Resultx get(){
        Resultx resx = new Resultx();

        Object obj = HttpUtil.get("http://localhost:8080/tests/get");
        resx = JsonUtil.fromJSON(obj.toString(),Resultx.class);

        return resx;
    }


}
